package com.feicheng;

import com.alibaba.fastjson.JSONObject;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class TimeRangeToBinaryString {

    public static String bitwiseOr(String jiaStr, String yiStr) {
        // 确保两个字符串长度相同
        if (jiaStr.length() != yiStr.length()) {
            throw new IllegalArgumentException("输入字符串长度必须相同");
        }

        StringBuilder result = new StringBuilder();

        // 按位比较
        for (int i = 0; i < jiaStr.length(); i++) {
            char jiaChar = jiaStr.charAt(i);
            char yiChar = yiStr.charAt(i);

            // 如果两个字符有一个是 '1'，则结果为 '1'，否则为 '0'
            if (jiaChar == '1' || yiChar == '1') {
                result.append('1');
            } else {
                result.append('0');
            }
        }

        return result.toString();
    }

    public static String bitwiseAnd(String jiaStr, String yiStr) {
        // 确保两个字符串长度相同
        if (jiaStr.length() != yiStr.length()) {
            throw new IllegalArgumentException("输入字符串长度必须相同");
        }

        StringBuilder result = new StringBuilder();

        // 按位比较
        for (int i = 0; i < jiaStr.length(); i++) {
            char jiaChar = jiaStr.charAt(i);
            char yiChar = yiStr.charAt(i);

            // 如果两个字符都是 '1'，则结果为 '1'，否则为 '0'
            if (jiaChar == '1' && yiChar == '1') {
                result.append('1');
            } else {
                result.append('0');
            }
        }

        return result.toString();
    }

    public static JSONObject generateTimeRangeBinary(LocalDateTime st, LocalDateTime et) {
        JSONObject result = new JSONObject();

        // 循环遍历从st到et的每一天
        LocalDateTime currentDayStart = st.truncatedTo(ChronoUnit.DAYS);
        while (!currentDayStart.isAfter(et)) {
            LocalDateTime currentDayEnd = currentDayStart.plusDays(1);

            // 生成当天的二进制字符串
            StringBuilder binaryString = new StringBuilder();
            for (int hour = 0; hour < 24; hour++) {
                for (int minute = 0; minute < 60; minute += 5) {
                    LocalDateTime currentTime = currentDayStart.plusHours(hour).plusMinutes(minute);

                    // 判断当前时间是否在st和et之间
                    if (!currentTime.isBefore(st) && !currentTime.isAfter(et)) {
                        binaryString.append("1");
                    } else {
                        binaryString.append("0");
                    }
                }
            }

            // 将当天的二进制字符串添加到结果中
            result.put(currentDayStart.toLocalDate().toString(), binaryString.toString());

            // 移动到下一天
            currentDayStart = currentDayEnd;
        }

        return result;
    }

    public static void main(String[] args) {
        LocalDateTime st = LocalDateTime.of(2023, 10, 1, 10, 15); // 开始时间
        LocalDateTime et = LocalDateTime.of(2023, 10, 1, 14, 45); // 结束时间

        JSONObject result = generateTimeRangeBinary(st, et);
        System.out.println(result.toJSONString()); // 打印格式化后的JSON
    }
}